﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using www.serviciipeweb.ro.iafblog.ExportDLL;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class SummaryForm : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        //private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        SummaryData m_Data;
        public DataRow dwABPOS;
        public DataSet dsParamaters;
        // string OrderNumber="";
        
        #endregion
        public SummaryForm()
        {
            InitializeComponent();
            this.Name = "报表";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           

            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new SummaryData(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void ProductionYarnSumm_Click(object sender, EventArgs e)
        {
            DataSet dsResult = new DataSet();
            SummaryDataInitDataBinding();

            //dicQueryParamater = new Dictionary<string, object>();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT Distinct '' as FACTORY,ABPAUFKRS||'-'||ABPAUFLFD||'-'||ABPAUFPOS AS ORDERNO, "
                    + " ABPAUFKRS,ABPAUFLFD,ABPAUFPOS,ABPSTARTDAT, "
                    + " TRIM(ABPART)||'-'||ABPVAR YARN,TRIM(GSAGARNNR)||TRIM(GSGARNBEZ)||TRIM(GSHANDLNR) AS DES, "
                    + " ABPFARBE,GSCBEZ,ABPSOLLMENGE5/100 AS WEI,  "
                    + " ABPLTVORL, '' as USAGE,'' as FABRIC, '' as COMMENTS "
                    + " FROM ABPOS,GSDAT,GSCOLOR WHERE ABPMANDANT = GSMANDANT AND ABPART = GSART AND ABPVAR = GSVAR "
                    + " AND ABPMANDANT = GSCMANDANT AND ABPART = GSCART AND ABPVAR = GSCVAR AND ABPFARBE = GSCFARBE "
                    + " AND ABPAUFART = 'GP' "//AND ABPERLKZ <>'J'  "
                    + " AND ABPSTARTDAT >" + this.STARTDATE.Text + " AND ABPSTARTDAT <" + this.ENDDATE.Text;
                //+ " and abpaufkrs = 'DB' and abpauflfd = '001213'  ";


                /*dicQueryParamater.Clear();
                dicQueryParamater.Add("CommandText", orlReadCommand.CommandText);
                dicQueryParamater.Add("CommandType", orlReadCommand.CommandType.ToString());
                dicQueryParamater.Add("ConnectionString", orlReadCommand.Connection.ConnectionString);
                foreach (OracleParameter cmp in orlReadCommand.Parameters)
                {
                    dicQueryParamater.Add(cmp.ParameterName.ToString(), cmp.Value.ToString());
                }
                dsResult = proxy.GetDataSetByQuery(dicQueryParamater);*/
                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABPOS";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["ABPOS"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["ABPOS"].Columns)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[dc.ColumnName] = value;

                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                    DataTable tableABPOS = setUI.Tables["ABPOS"];
                    foreach (DataRow rowABPOS in tableABPOS.Rows)
                    {
                        setUI = m_Data.GetABFOLGE(setUI, rowABPOS);
                        setUI = m_Data.GetABAUFM(setUI, rowABPOS);
                        string YarnOrder = rowABPOS["ABPAUFKRS"].ToString() + rowABPOS["ABPAUFLFD"].ToString() + rowABPOS["ABPAUFPOS"].ToString();
                        rowABPOS["COMMENTS"] = m_Data.GetCommentsFromTXTIND("ABP", YarnOrder);
                    } 
                } 
                setUI.Tables["ABPOS"].ExportTo(ExportToFormat.Excel2007, @"C:\intex.w\Report\ProductionYarnSumm.xlsx");
                System.Diagnostics.Process.Start("EXCEL.EXE", "C:\\intex.w\\Report\\ProductionYarnSumm.xlsx");
                return;

            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void SummaryDataInitDataBinding()
        { 

            this.setUI.Tables.Clear(); 
            setUI.AcceptChanges();
        }

        private void ProductionYarnSumm_Click(object sender, RoutedEventArgs e)
        {

        } 
    }
}
